<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="15vh"
    width="800px"
  >
    <div class="content">
      <div class="title">
        <el-amap-search-box
          :on-search-result="onSearchResult"
          :search-option="searchOption"
          class="search-box"
          style="width: 100%"
        ></el-amap-search-box>
      </div>
      <el-amap :center="mapCenter" :events="events" :zoom="12" class="amap">
        <el-amap-marker
          v-for="(marker, index) in markers"
          :key="index"
          :draggable="marker.draggable"
          :events="marker.events"
          :position="marker.position"
          :visible="marker.visible"
        ></el-amap-marker>
      </el-amap>
      <div class="toolbar">地址:{{ address }}</div>
    </div>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    let self = this;
    return {
      // 数据
      globalData: {
        title: "",
      },
      // 显示地址
      address: "",
      //   搜索条件
      searchOption: {},
      //   初始化中心地址
      mapCenter: [116.4074, 39.9042],
      markers: [
        {
          position: [116.4074, 39.9042],
        },
      ],
      events: {
        click(e) {
          let { lng, lat } = e.lnglat;
          self.markers[0].position = self.mapCenter = [lng, lat];
          // 这里通过高德 SDK 完成。
          var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all",
          });
          geocoder.getAddress([lng, lat], function (status, result) {
            if (status === "complete" && result.info === "OK") {
              if (result && result.regeocode) {
                self.address = result.regeocode.formattedAddress;
              }
            }
          });
        },
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: {
        visible: true,
      },
    };
  },

  created() {
    this.markers[0].position = this.mapCenter = this.info.mapCenter;
    this.address = this.info.address;
    this.globalData.title = "选择地图";
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$emit("close", {
        address: this.address,
        mapCenter: this.mapCenter,
      });
    },
    // 搜索内容
    onSearchResult(pois) {
      let { lng, lat } = pois[0];
      if (pois.length > 0) {
        this.markers[0].position = this.mapCenter = [lng, lat];
        this.getAddress();
      }
    },
    getAddress() {
      let that = this;
      var geocoder = new AMap.Geocoder({
        radius: 1000,
        extensions: "all",
      });
      geocoder.getAddress(
        [this.mapCenter[0], this.mapCenter[1]],
        function (status, result) {
          if (status === "complete" && result.info === "OK") {
            that.address = result.regeocode.formattedAddress;
          }
        }
      );
    },
    // 关闭
    handleClose() {
      this.$emit("close", {});
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .tox-tinymce {
  min-height: 500px;
}

.form {
  .item {
    border: 1px dashed #c0c4cc;
    margin-bottom: 10px;
    padding: 10px;

    .form-item {
      display: grid;
      grid-template-columns: 100px auto;
      align-items: center;
    }

    .name {
      font-weight: bold;
      font-size: 15px;
    }
  }
}

.amap {
  height: 400px;
}

.content {
  position: relative;

  .title {
    width: 500px;
    position: absolute;
    top: 0;
    left: 0;
  }

  .toolbar {
    line-height: 60px;
    font-weight: bold;
    font-size: 14px;
  }
}
</style>
